package com.android.kvdb;

import android.annotation.TargetApi;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.util.Log;

import java.io.File;

/**
 * @author GYL
 * @dateTime 2020/5/12-10:18
 * @description
 */
public class DbHelper extends SQLiteOpenHelper {

    private static final String TAG = "flueky";

    /**
     * @see SQLiteDatabase#openDatabase(String, SQLiteDatabase.CursorFactory, int)
     * @param context
     * @param name
     * @param factory
     * @param version
     */
    public DbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    /**
     * @see SQLiteDatabase#openDatabase(String, SQLiteDatabase.CursorFactory, int, DatabaseErrorHandler)
     * @param context
     * @param name
     * @param factory
     * @param version
     * @param errorHandler
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public DbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    /**
     * @see SQLiteDatabase#openDatabase(String , OpenParams);
     * @param context
     * @param name
     * @param version
     * @param openParams
     */
    @TargetApi(Build.VERSION_CODES.P)
    public DbHelper(Context context, String name, int version, SQLiteDatabase.OpenParams openParams) {
        super(context, name, version, openParams);
    }

    // 创建数据文件时调用，此时适合创建新表
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_SQL = "create table user( id INTEGER primary key autoincrement,name text,age INTEGER)";
        Log.d(TAG, "create sql : " + CREATE_SQL);
        db.execSQL(CREATE_SQL);
    }

    // 更新数据库版本时调用，适合更新表结构或创建新表
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
